package timetable.student;

import static android.provider.BaseColumns._ID;
import static timetable.student.Constants.DAY;
import static timetable.student.Constants.LECTURER;
import static timetable.student.Constants.MODULE;
import static timetable.student.Constants.ROOM;
import static timetable.student.Constants.TABLE_NAME;
import static timetable.student.Constants.TIMEEND;
import static timetable.student.Constants.TIMESTART;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.RemoteViews;

public class Widget extends AppWidgetProvider {
	private static final int ADD_CLASS = 0;
	private static String[] FROM = { _ID, DAY, MODULE, LECTURER,ROOM,TIMESTART,TIMEEND };
	private static String ORDER_BY = DAY+", "+TIMESTART + " ASC";
	private static String WHERE = DAY;
	private TimeTableData events;
	 SQLiteDatabase db ;
	 private SimpleDateFormat formatter = new SimpleDateFormat( 
     "EEEEEEEEE");
	 private SimpleDateFormat formatters = new SimpleDateFormat( 
     "HH:mm");
	@Override
	public void onEnabled(Context context)
	{
		
		
		
		
	}
	 
	 
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
		String now = formatter.format(new Date());
		int day = getDayInt(now);
		String timeNow = formatters.format(new Date());
		
		
		events = new TimeTableData(context); 
		db = events.getReadableDatabase();
		String nxt = getEvents(context,timeNow,day);
		
		// Change the text in the widget
	      RemoteViews updateViews = new RemoteViews( 
	            context.getPackageName(), R.layout.widgetlayout);
	      updateViews.setTextViewText(R.id.text, "Your Next Class is\n"+nxt); 
	      appWidgetManager.updateAppWidget(appWidgetIds, updateViews); 
	      
	      // Not really necessary, just a habit
	      super.onUpdate(context, appWidgetManager, appWidgetIds); 
	}
	
	private String getEvents(Context context,String time,int today) {
        // Perform a managed query. The Activity will handle closing
        // and re-querying the cursor when needed.
       
        Cursor cursor = db.query(TABLE_NAME, FROM, TIMESTART+">? AND "+DAY+"=?", new String[]{time,Integer.toString(today)}, null,
              null, ORDER_BY,"1");
        String nxt = "";
        while(cursor.moveToNext())
        {
            int id = cursor.getInt(0);
           int day = cursor.getInt(1);
           String module = cursor.getString(2);
           String lectuerer = cursor.getString(3);
           String room = cursor.getString(4);
           String startTime = cursor.getString(5);
           String endTime = cursor.getString(6);
         nxt = startTime + " - "+module+" - "+room;
        }
        return nxt;
     }
private void showEvents(Cursor cursor) {
    while (cursor.moveToNext()) { 
    	int id = cursor.getInt(0);
       int day = cursor.getInt(1);
       String module = cursor.getString(2);
       String lectuerer = cursor.getString(3);
       String room = cursor.getString(4);
       String startTime = cursor.getString(5);
       String endTime = cursor.getString(6);
      
    }
    // Display on the screen
    

 }
	
	public int getDayInt(String day)
	{
		String[] d = new String[] {"Monday","Tuesday","Wednesday","Thursday","Friday"};
		for(int i = 0; i < d.length;++i)
		{
			if(day.equals(d[i]))
			{
				return i+1;
			}
		}
		
		return 0;
		
	}
}
